//
//  NSString+GQAdd.m
//  GQCategorys
//
//  Created by 走出趣_甄冠卿 on 2018/8/14.
//  Copyright © 2018年 zhenguanqing. All rights reserved.
//

#import "NSString+GQAdd.h"

@implementation NSString (GQAdd)

- (CGFloat)widthForFont:(UIFont *)font {
    CGSize size = [self sizeForFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.width;
}

- (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width {
    CGSize size = [self sizeForFont:font size:CGSizeMake(width, HUGE) mode:NSLineBreakByWordWrapping];
    return size.height;
}

- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    CGSize result;
    if (!font) font = [UIFont systemFontOfSize:12];
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSMutableDictionary *attr = [NSMutableDictionary new];
        attr[NSFontAttributeName] = font;
        if (lineBreakMode != NSLineBreakByWordWrapping) {
            NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
            paragraphStyle.lineBreakMode = lineBreakMode;
            attr[NSParagraphStyleAttributeName] = paragraphStyle;
        }
        CGRect rect = [self boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:attr context:nil];
        result = rect.size;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        result = [self sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
#pragma clang diagnostic pop
    }
    return result;
}

- (BOOL)isHaveString:(NSString *)subString {
    NSRange _range = [subString rangeOfString:self];
    if (_range.location != NSNotFound) {
        return YES;
    } else {
        return NO;
    }
}

- (NSArray *)getRangeOfString:(NSString *)subString {
    NSMutableArray *rangesInfo = [NSMutableArray array];
    NSString *sourceString = [self copy];
    NSInteger location = 0;
    NSInteger count = sourceString.length;
    for (int i = 0; i < count; i++) {
        NSRange range = [sourceString rangeOfString:subString];
        if (range.location != NSNotFound) {
            NSDictionary *dict = @{@"loc":@(range.location + location),@"len":@(range.length)};
            [rangesInfo addObject:dict];
            location = location + range.location + range.length;
            sourceString = [sourceString substringFromIndex:range.location+range.length];
        } else {
            break;
        }
    }
    
    return rangesInfo;
}

+ (NSDictionary *)getParametersFormURLString:(NSString *)urlString {
    if (urlString && urlString.length && [urlString rangeOfString:@"?"].length == 1) {
        NSArray *array = [urlString componentsSeparatedByString:@"?"];
        if (array && array.count == 2) {
            NSString *paramsStr = array[1];
            if (paramsStr.length) {
                NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
                NSArray *paramArray = [paramsStr componentsSeparatedByString:@"&"];
                for (NSString *param in paramArray) {
                    if (param && param.length) {
                        NSArray *parArr = [param componentsSeparatedByString:@"="];
                        if (parArr.count == 2) {
                            [paramsDict setObject:parArr[1] forKey:parArr[0]];
                        }
                    }
                }
                return [paramsDict copy];
            }else{
                return nil;
            }
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}

- (NSString *)hideTrueName {
    NSString *showName = @"";
    NSString *showPart = [self substringFromIndex:self.length - 1];
    for (int i = 0; i < self.length - 1; i++) {
        showName = [showName stringByAppendingString:@"*"];
    }
    return [showName stringByAppendingString:showPart];
}

- (NSDictionary *)convertToJsonDictionary {
    
    NSString *jsonString = self;
    
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

@end
